class CANVAS_EXAMPLE
-- This example show how draw basic primitives at IUP_CANVAS (marks, 
-- lines, polygons, filled areas and text).
	
inherit
	IUP_INTERFACE

create {ANY}
   make

feature {ANY}

	angle: REAL_64
	canv: IUP_CANVAS

   make
      local
 			gui: IUP
			i: STRING
			a: ARRAY[IUP_WIDGET]
			l: IUP_LABEL
			b: IUP_BUTTON
			v: IUP_VBOX
			w: IUP_DIALOG

      do
			gui := iup_open

			create canv.canvas
			canv.set_size(300, 150)
			canv.set_cb_map(agent map_canvas)
			canv.set_cb_canvas_action(agent draw)
			canv.set_cb_button(agent button)
			
			create l.label("First test drawing with IUP_CANVAS!")
			create b.button("Rotate 5 degrees")
			b.set_cb_action(agent rotate_canvas)
			
			a := {ARRAY[IUP_WIDGET]} << l, b, canv >>

	      create v.vbox(a)
	      v.set_alignment("ACENTER")
			v.set_gap(10)
			v.set_margin(10, 10)
			
	      create w.dialog(v)
			w.set_title("CD test")
			w.set_raster_size(520, 470)

			i := w.show

			gui.main_loop
			gui.close
      end

	map_canvas (widget: IUP_CANVAS): STRING
		-- Create the internal canvas (CD_IUP) when the widget
		-- is mapped (IUP_CANVAS)
		local
			canvas: CD_IUP
		do
			create canvas.create_double_buffer_with_canvas(widget)
			
			Result := "IUP_DEFAULT"
		end

	draw (widget: IUP_CANVAS; x, y: REAL_32): STRING
		local
			ok: BOOLEAN
			canvas: CD_IUP
		do
			canvas := widget.get_cd_canvas
			ok := canvas.activate

			if ok then
				-- Rotate
				canvas.apply_transform_rotate(angle)
				-- Clear the canvas with a white background
				canvas.set_predefined_background_color(canvas.cd_white)
				canvas.clear

				canvas.set_interior_style_solid
				-- Draw two lines
				canvas.set_line_width(3)
				canvas.set_predefined_foreground_color(canvas.cd_blue)
				canvas.draws_line(40, 40, 450, 40)
				canvas.draws_line(45, 65, 455, 65)
				-- Draw a rect with dotted line
				canvas.set_line_style_dotted
				canvas.set_predefined_foreground_color(canvas.cd_red)
				canvas.draws_rect(60, 200, 80, 175)
				-- Draw an arc
				canvas.set_line_style_continuous
				canvas.draws_arc(150, 200, 100, 50, 0, 150)
				-- Draw a box (filled rect)
				canvas.set_predefined_foreground_color(canvas.cd_yellow)
				canvas.draws_box(240, 470, 80, 175)
				-- Draw a sector (filled arc)
				canvas.set_predefined_foreground_color(canvas.cd_magenta)
				canvas.draws_sector(260, 200, 100, 50, 0, 150)
				-- Draw a text
				--canvas.set_text_orientation(20)
				canvas.draws_text(370, 200, "Eiffel IUP")
				-- Test of polygons
				canvas.set_line_width(1)
				-- Draw an open polygon
				canvas.set_predefined_foreground_color(canvas.cd_dark_red)
				canvas.begin_open_lines
				canvas.vertex(50,250)
				canvas.vertex(80,340)
				canvas.vertex(150,300)
				canvas.vertex(110,265)
				canvas.vertex(90,290)
				canvas.end_polygon
				-- Draw a closed polygon
				canvas.set_predefined_foreground_color(canvas.cd_cyan)
				canvas.begin_closed_lines
				canvas.vertex(200,250)
				canvas.vertex(230,340)
				canvas.vertex(300,300)
				canvas.vertex(260,265)
				canvas.vertex(240,290)
				canvas.end_polygon
				-- Draw a filled polygon
				canvas.set_predefined_foreground_color(canvas.cd_black)
				canvas.set_line_style_continuous
				canvas.set_hatch_backward_diagonal
				canvas.begin_fill
				canvas.vertex(350,250)
				canvas.vertex(380,340)
				canvas.vertex(450,300)
				canvas.vertex(410,265)
				canvas.vertex(390,290)
				canvas.end_polygon
				-- Draw three marks
				canvas.set_predefined_foreground_color(canvas.cd_green)
				canvas.draws_mark(100, 350)
				canvas.draws_mark(260, 156)
				canvas.draws_mark(358, 289)
			
				canvas.flush
			end
			
			Result := "IUP_DEFAULT"
		end

	button (widget: IUP_CANVAS; btn, prs, x, y: INTEGER; status: STRING): STRING
		-- Draws a mark at click coordinates.
		local
			--i: INTEGER
			--tup: TUPLE[INTEGER, INTEGER, INTEGER, INTEGER, INTEGER, INTEGER, INTEGER, INTEGER]
			ok: BOOLEAN
			canvas: CD_IUP
		do
			canvas := widget.get_cd_canvas
			ok := canvas.activate
			--i := canvas.get_font_size
			--tup := canvas.get_text_bounds(100, 100, "Hello")

			if ok then
				canvas.set_predefined_foreground_color(canvas.cd_green)
				canvas.draws_mark(x, canvas.update_y_axis(y))
				canvas.flush
			end

			--io.put_integer(i)
			--io.put_new_line
			--io.put_string(tup.item_1.to_string + " " + tup.item_2.to_string + " " + tup.item_3.to_string + " " + tup.item_4.to_string + " " + tup.item_5.to_string + " " + tup.item_6.to_string + " " + tup.item_7.to_string + " " + tup.item_8.to_string)
			--io.put_new_line
			
			Result := "IUP_DEFAULT"
		end

	rotate_canvas (widget: IUP_BUTTON): STRING
		do
			angle := (5).to_double
			canv.redraw
						
			Result := "IUP_DEFAULT"
		end

end -- class CAVAS_EXAMPLE
